# -*-coding:utf-8 -*-

class HanderZnzIndex:
    def __init__(self):
        pass

    def set_pd_data(self, pd_odata):
        self.o_data = pd_odata

    def _handel_data(self):
        """
        处理0号指标
        """
        # 数据转化为 float类型
        self.o_data["turn"] = [0 if x == "" else float(
            x) for x in self.o_data["turn"]]
        self.o_data["volume"] = [0 if x == "" else float(
            x) for x in self.o_data["volume"]]
        self.o_data["amount"] = [0 if x == "" else float(
            x) for x in self.o_data["amount"]]
              
        self.o_data["open"] = [0 if x == "" else float(
            x) for x in self.o_data["open"]]
        self.o_data["close"] = [0 if x == "" else float(
            x) for x in self.o_data["close"]]
        self.o_data["high"] = [0 if x == "" else float(
            x) for x in self.o_data["high"]]
        self.o_data["low"] = [0 if x == "" else float(
            x) for x in self.o_data["low"]]

    def index_0(self):
        self._handel_data()

        # 开盘价0号指标
        self.o_data['open_0'] = self.o_data.apply(
            lambda x: 0 if x['turn'] == 0 else x['volume']*100*x['open']/x['turn'], axis=1)

        # 收盘价0号指标
        self.o_data['close_0'] = self.o_data.apply(
            lambda x: 0 if x['turn'] == 0 else x['volume']*100*x['close']/x['turn'], axis=1)

        # 最高价0号指标
        self.o_data['high_0'] = self.o_data.apply(
            lambda x: 0 if x['turn'] == 0 else x['volume']*100*x['high']/x['turn'], axis=1)

        # 最低价0号指标
        self.o_data['low_0'] = self.o_data.apply(
            lambda x: 0 if x['turn'] == 0 else x['volume']*100*x['low']/x['turn'], axis=1)
        #流通盘
        self.o_data['tq_0']=self.o_data.apply(
            lambda x: 0 if x['turn'] == 0 else x['volume']*100/x['turn'], axis=1)

        index_data = {
            'date':  self.o_data.iloc[0]['date'],
            'open_0': self.o_data['open_0'].sum(),
            'close_0': self.o_data['close_0'].sum(),
            'high_0': self.o_data['high_0'].sum(),
            'low_0': self.o_data['low_0'].sum(),
            'tq_0': self.o_data['tq_0'].sum(),
            "vol_0":self.o_data['volume'].sum(),
            "amount_0":self.o_data['amount'].sum(), #成交额
        }

        self.index_0_data=index_data

        return index_data
        # print(self.o_data['open_0'].sum(), self.o_data['close_0'].sum(), self.o_data['high_0'].sum(), self.o_data['low_0'].sum())

    def index_0A(self):
        """
        指南针0A指数
        """
        index_0A_data={
            'date':  self.index_0_data['date'],
            'open_0A': self.index_0_data['open_0']/self.index_0_data['tq_0'],
            'close_0A': self.index_0_data['close_0']/self.index_0_data['tq_0'],
            'high_0A': self.index_0_data['high_0']/self.index_0_data['tq_0'],
            'low_0A': self.index_0_data['low_0']/self.index_0_data['tq_0'],
            'tq_0': self.index_0_data['tq_0'],
            'vol_0':self.index_0_data['vol_0'],
            "amount_0":self.index_0_data['amount_0'],
        }
        return index_0A_data